﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using tracker.Models.DTO;
using tracker.Models;
using tracker.Models.DAL;
using System.Collections.Generic;

namespace tracker.Models.BLL
{
    public class TrackLogBLL : ErpCatalogueBaseBL
    {
        #region STORE DEFINE
        public const string STORE_INSERT = "";
        public const string STORE_UPDATE = "";
        public const string STORE_DELETE = "";
        public const string STORE_SELECT_ALL = "";
        public const string STORE_REPORT_SEARCH_ALARM = "searchAlarmDevice";
        public const string STORE_REPORT_SEARCH_ALARM_COUNT = "countsearchAlarmDevice";
        public const string STORE_REPORT_SEARCH_HISTORY_MORE = "searchInformationDeviceMoreThan";
        public const string STORE_REPORT_SEARCH_HISTORY_MORE_COUNT = "countsearchInformationDeviceMoreThan";
        public const string STORE_REPORT_SEARCH_HISTORY_LESS = "searchInformationDeviceLessThan";
        public const string STORE_REPORT_SEARCH_HISTORY_LESS_COUNT = "countsearchInformationDeviceMoreThan";
        public const string STORE_REPORT_JOURNEY_FROM = "getJourneyFromTime";
        public const string STORE_REPORT_JOURNEY_TO = "getJourneyToTime";
        public const string STORE_REPORT_TOTALSPEEDINFOCIRCLE = "TrackLog_TotalSpeedInfoCircle";
        public const string STORE_REPORT_LINECHART_MORETHAN = "searchInformationDeviceMoreThanLineChart";
        public const string STORE_REPORT_LINECHART_LESSTHAN = "searchInformationDeviceLessThanLineChart";
        public const string STORE_REPORT_LINECHART_MORETHAN_COUNT = "countsearchInformationDeviceMoreThanLineChart";
        public const string STORE_REPORT_LINECHART_LESSTHAN_COUNT = "countsearchInformationDeviceLessThanLineChart";
        
        #endregion

        #region Methods
        /// <summary>
        /// Hàm này thêm mới một ROW thông qua 1 mảng tham số và 1 mảng giá trị với tham số
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns>string</returns>
        public static string InsertERP(string[] arrParamName, object[] arrParamValue)
        {
            string strReturn = "0";
            strReturn = Insert(STORE_INSERT, arrParamName, arrParamValue);
            return strReturn;
        }

        /// <summary>
        /// Hàm này update một ROW thông qua 1 mảng tham số và 1 mảng giá trị với tham số
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static string UpdateERP(string[] arrParamName, object[] arrParamValue)
        {
            string strReturn = "0";
            strReturn = Update(STORE_UPDATE, arrParamName, arrParamValue);
            return strReturn;
        }

        /// <summary>
        /// Hàm này delete ROW thông qua 1 mảng tham số và 1 mảng giá trị với tham số
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static string DeleteERP(string[] arrParamName, object[] arrParamValue)
        {
            string strReturn = "0";
            strReturn = Delete(STORE_DELETE, arrParamName, arrParamValue);
            return strReturn;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static DataSet SelectAll(string[] arrParamName, object[] arrParamValue)
        {
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_SELECT_ALL, arrParamName, arrParamValue);
            return ds;
        }

        /// <summary>
        /// Hàm này trả về dữ liệu cho report search alarm
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static DataSet GetRecodeBySearchAlarm(string[] arrParamName, object[] arrParamValue)
        {
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_SEARCH_ALARM, arrParamName, arrParamValue);
            return ds;
        }

        /// <summary>
        /// Hàm này trả về dữ liệu count cho report search alarm
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static DataSet GetTotalRecordBySearchAlarm(string[] arrParamName, object[] arrParamValue)
        {
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_SEARCH_ALARM_COUNT, arrParamName, arrParamValue);
            return ds;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static List<ETrackLog> GetRecodeBySearchSpeedAlarmMoreThan(string[] arrParamName, object[] arrParamValue)
        {
            List<ETrackLog> list = new List<ETrackLog>();
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_SEARCH_HISTORY_MORE, arrParamName, arrParamValue);
            foreach (DataRow idr in ds.Tables[0].Rows)
            {
                ETrackLog ent = GetOneTrackLog(idr);
                if (idr["DeviceName"] != DBNull.Value)
                    ent.DeviceName = (string)idr["DeviceName"];

                list.Add(ent);
            }
            return list;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static DataSet GetRecodeBySearchSpeedAlarmMoreThanExcel(string[] arrParamName, object[] arrParamValue)
        {
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_SEARCH_HISTORY_MORE, arrParamName, arrParamValue);
            return ds;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static List<ETrackLog> GetRecodeBySearchSpeedAlarmLessThan(string[] arrParamName, object[] arrParamValue)
        {
            List<ETrackLog> list = new List<ETrackLog>();
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_SEARCH_HISTORY_LESS, arrParamName, arrParamValue);
            foreach (DataRow idr in ds.Tables[0].Rows)
            {
                ETrackLog ent = GetOneTrackLog(idr);
                if (idr["DeviceName"] != DBNull.Value)
                    ent.DeviceName = (string)idr["DeviceName"];

                list.Add(ent);
            }
            return list;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static DataSet GetRecodeBySearchSpeedAlarmLessThanExcel(string[] arrParamName, object[] arrParamValue)
        {
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_SEARCH_HISTORY_LESS, arrParamName, arrParamValue);
            return ds;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static DataSet GetTotalRecordBySearchSpeedAlarmMoreThan(string[] arrParamName, object[] arrParamValue)
        {
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_SEARCH_HISTORY_MORE_COUNT, arrParamName, arrParamValue);
            return ds;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static DataSet GetTotalRecordBySearchSpeedAlarmLessThan(string[] arrParamName, object[] arrParamValue)
        {
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_SEARCH_HISTORY_LESS_COUNT, arrParamName, arrParamValue);
            return ds;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static DataSet GetJourneyToTime(string[] arrParamName, object[] arrParamValue)
        {
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_JOURNEY_TO, arrParamName, arrParamValue);
            return ds;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static DataSet GetJourneyFromTime(string[] arrParamName, object[] arrParamValue)
        {
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_JOURNEY_FROM, arrParamName, arrParamValue);
            return ds;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static int GetTotalSpeedInfoCricleChart(string[] arrParamName, object[] arrParamValue)
        {
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_TOTALSPEEDINFOCIRCLE, arrParamName, arrParamValue);
            if (ds.Tables[0].Rows.Count > 0)
                return int.Parse(ds.Tables[0].Rows[0]["Total"].ToString());
            else
                return 0;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static List<ETrackLog> GetRecodeBySearchSpeedAlarmMoreThanLineChart(string trackName, string[] arrParamName, object[] arrParamValue)
        {
            List<ETrackLog> list = new List<ETrackLog>();
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_LINECHART_MORETHAN, arrParamName, arrParamValue);
            foreach (DataRow idr in ds.Tables[0].Rows)
            {
                ETrackLog ent = GetOneTrackLogLineChart(idr);
                if (trackName.Length > 0)
                    ent.DeviceName = (string)idr["DeviceName"];

                list.Add(ent);
            }

            return list;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static List<ETrackLog> GetRecodeBySearchSpeedAlarmLessThanLineChart(string trackName, string[] arrParamName, object[] arrParamValue)
        {
            List<ETrackLog> list = new List<ETrackLog>();
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_LINECHART_LESSTHAN, arrParamName, arrParamValue);
            foreach (DataRow idr in ds.Tables[0].Rows)
            {
                ETrackLog ent = GetOneTrackLogLineChart(idr);
                if (trackName.Length > 0)
                    ent.DeviceName = (string)idr["DeviceName"];

                list.Add(ent);
            }

            return list;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static DataSet GetTotalRecordBySearchSpeedAlarmMoreThanLineChart(string[] arrParamName, object[] arrParamValue)
        {
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_LINECHART_MORETHAN_COUNT, arrParamName, arrParamValue);
            return ds;
        }

        /// <summary>
        /// Hàm này trả về danh sách các row theo điều kiện đầu vào
        /// </summary>
        /// <param name="arrParamName">Array of Parameters</param>
        /// <param name="arrParamValue">Array of Values</param>
        /// <returns></returns>
        public static DataSet GetTotalRecordBySearchSpeedAlarmLessThanLineChart(string[] arrParamName, object[] arrParamValue)
        {
            DataSet ds = new DataSet();
            ds = SelectAllWithParameter(STORE_REPORT_LINECHART_LESSTHAN_COUNT, arrParamName, arrParamValue);
            return ds;
        }

        /// <summary>
        /// Get Track Log
        /// </summary>
        /// <param name="idr"></param>
        /// <returns></returns>
        private static ETrackLog GetOneTrackLog(DataRow idr)
        {
            ETrackLog OTrackLog = new ETrackLog();
            if (idr["TrackID"] != DBNull.Value)
                OTrackLog.TrackID = (long)idr["TrackID"];
            if (idr["RecieveTime"] != DBNull.Value)
                OTrackLog.RecieveTime = (DateTime)idr["RecieveTime"];
            if (idr["DeviceKey"] != DBNull.Value)
                OTrackLog.DeviceKey = (string)idr["DeviceKey"];
            if (idr["Header"] != DBNull.Value)
                OTrackLog.Header = (string)idr["Header"];
            if (idr["Length"] != DBNull.Value)
                OTrackLog.Length = (string)idr["Length"];
            if (idr["CMD"] != DBNull.Value)
                OTrackLog.CMD = (string)idr["CMD"];
            if (idr["Crc"] != DBNull.Value)
                OTrackLog.Crc = (string)idr["Crc"];
            if (idr["EndPacket"] != DBNull.Value)
                OTrackLog.EndPacket = (string)idr["EndPacket"];
            if (idr["Gps_time"] != DBNull.Value)
                OTrackLog.Gps_time = (DateTime)idr["Gps_time"];
            if (idr["Gps_status"] != DBNull.Value)
                OTrackLog.Gps_status = (string)idr["Gps_status"];
            if (idr["Latitude"] != DBNull.Value)
                OTrackLog.Latitude = (string)idr["Latitude"];
            if (idr["Longitude"] != DBNull.Value)
                OTrackLog.Longitude = (string)idr["Longitude"];
            if (idr["AlarmTypeID"] != DBNull.Value)
                OTrackLog.AlarmCode = idr["AlarmTypeID"].ToString();
            if (idr["Speed"] != DBNull.Value)
                OTrackLog.Speed = (string)idr["Speed"];
            if (idr["Heading"] != DBNull.Value)
                OTrackLog.Heading = (string)idr["Heading"];
            if (idr["Magnetic_variation"] != DBNull.Value)
                OTrackLog.Magnetic_variation = (string)idr["Magnetic_variation"];
            if (idr["Direction"] != DBNull.Value)
                OTrackLog.Direction = (string)idr["Direction"];
            if (idr["Crc_gprmc"] != DBNull.Value)
                OTrackLog.Crc_gprmc = (string)idr["Crc_gprmc"];
            if (idr["Hdop"] != DBNull.Value)
                OTrackLog.Hdop = (string)idr["Hdop"];
            if (idr["Altitude"] != DBNull.Value)
                OTrackLog.Altitude = (string)idr["Altitude"];
            if (idr["State"] != DBNull.Value)
                OTrackLog.State = (string)idr["State"];
            if (idr["AD"] != DBNull.Value)
                OTrackLog.AD = (string)idr["AD"];
            if (idr["Baseid"] != DBNull.Value)
                OTrackLog.Baseid = (string)idr["Baseid"];
            if (idr["Csq"] != DBNull.Value)
                OTrackLog.Csq = (string)idr["Csq"];
            if (idr["Journey"] != DBNull.Value)
                OTrackLog.Journey = (string)idr["Journey"];
            //if (idr["Packet"] != DBNull.Value)
            //    OTrackLog.Packet = (string)idr["Packet"];
            if (idr["Angle"] != DBNull.Value)
                OTrackLog.Angle = (string)idr["Angle"];
            if (idr["Temperature"] != DBNull.Value)
                OTrackLog.Temperature = (string)idr["Temperature"];
            if (idr["High"] != DBNull.Value)
                OTrackLog.High = (string)idr["High"];
            if (idr["Picture"] != DBNull.Value)
                OTrackLog.Picture = (string)idr["Picture"];
            if (idr["Note"] != DBNull.Value)
                OTrackLog.Note = (string)idr["Note"];
            if (idr["RunTime"] != DBNull.Value)
                OTrackLog.RunTime = (string)idr["RunTime"];
            if (idr["DeviceKeySecurity"] != DBNull.Value)
                OTrackLog.DeviceKeySecurity = (string)idr["DeviceKeySecurity"];
            if (idr["Gsm_Signal"] != DBNull.Value)
                OTrackLog.Gsm_Signal = (string)idr["JouGsm_Signalrney"];
            if (idr["Satellites"] != DBNull.Value)
                OTrackLog.Satellites = (string)idr["Satellites"];

            return OTrackLog;
        }
        #endregion

        private static ETrackLog GetOneTrackLogLineChart(DataRow idr)
        {
            ETrackLog OTrackLog = new ETrackLog();

            if (idr["Gps_time"] != DBNull.Value)
                OTrackLog.Gps_time = DateTime.Parse(idr["Gps_time"].ToString());
            if (idr["Speed"] != DBNull.Value)
                OTrackLog.Speed = (string)idr["Speed"];

            return OTrackLog;
        }
    }
}
